import test.*;
import seunit.framework.*;
import seunit.framework.automation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.lang.reflect.InvocationTargetException;
import java.io.*;
import java.text.*;
import java.lang.reflect.Method;

import org.junit.rules.ErrorCollector;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import jxl.Sheet;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.util.*;

import javax.security.auth.login.LoginContext;




/**
 * 
 * Runner.java
 * Purpose: Runs the test cases.
 *
 * @author Vishal Sangave
 * @version 1.0 29 May 2012
 */

@SuppressWarnings("unused")
public class Runner{


private File inputWorkbook;
static PrintWriter pw;
static ArrayList<String> list;
  /**
     * The main method begins execution of the tests.
     *
     * @param args not used
     */
Integer i=0;
public void runTests(Object suiteObj) throws Exception {
	Method m;
	String f="0";
	try{
	m = suiteObj.getClass().getDeclaredMethod("setUp");
	m.setAccessible(true);
	m.invoke(suiteObj);
	}catch (Exception e) {
		System.out.println("\nException :"+e);
	}
	Iterator<String> testToExecute = list.iterator();
	//while(testToExecute. hasNext()){        
        //System.out.println(testToExecute.next());    
    //}
	Class<?> cl = suiteObj.getClass();
	Iterable<Method> met = TestSuite.getAllDeclaredMethods(cl);
	for(Method metobj : met) {
		f="0";
		long startTime = System.currentTimeMillis();
       // System.out.println(metobj.getName());
        if(list.contains(metobj.getName())){
        	metobj.setAccessible(true);
        	try{
        		metobj.invoke(suiteObj);
        	}catch(ElementNotVisibleException e){
        		debug.getError(e);
        	}catch(Exception e){
        		debug.getError(e);
        	}
        	long endTime = System.currentTimeMillis();
        	pw.print("<TR>");
        	pw.print("<TD>" + (i++) + "<TD>"+metobj.getName());
        	if(f=="0")
        	{
        		pw.print("<TD>Pass");
        		pw.print("<TD>Sucessfuly executed");
        	}
        	else
        	{
        		pw.print("<TD>Fail");
        		pw.print("<TD>"+f);
        	}
			long Time=(endTime-startTime);
			pw.print("<TD>"+Time);
			pw.print("</TR>");
        }
     }
	m = suiteObj.getClass().getDeclaredMethod("tearDown");
	m.setAccessible(true);
	m.invoke(suiteObj);
}

	public static void main(String[] args)  {
		try{
			LinkedList<Object> suites = new LinkedList<Object>();
			SuiteSignUp objSuiteSignUp = new SuiteSignUp();
			SuiteTestManagement objSuiteTestManagement = new SuiteTestManagement();
			suites.add(objSuiteTestManagement);
			suites.add(objSuiteSignUp);
			
		Runner obj = new Runner();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
		df.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
		Calendar currentDate = Calendar.getInstance();
		String reportname = df.format(currentDate.getTime());
		list= (ArrayList<String>) util.readList(".\\data\\ExecutionList.xls");
		pw = new PrintWriter(new FileWriter(".\\report\\Report"+reportname+".html"));
	    pw.println("<html><head><link media='screen' type='text/css' href='style.css' rel='stylesheet'></head<body><table summary='Test Case summary report' id='rounded-corner'><thead><tr><th class='rounded-company' scope='col'>TC_ID</th><th  scope='col'>Test Case Name</th><th  scope='col'>Status</th><th  scope='col'>Message</th><th class='rounded-q4' scope='col'>Time</th></tr></thead><tfoot><tr><td class='rounded-foot-left' colspan='4'><em>These test cases are Exected in Time</em></td><td class='rounded-foot-right'>&nbsp;</td></tr></tfoot><tbody>");
	    
			Iterable<Object> suite= suites;
			Iterator<Object> iterator = suite.iterator();
			while(iterator. hasNext()){
	            obj.runTests(iterator.next());
	        }
		pw.print("</tbody></TABLE></body></html>");
		pw.close();
		if(log.error.length() > 0)
		{
			System.out.println("\nErrors :"+log.error);
		}
		if(log.failure.length() > 0)
		{
			System.out.println("\nFailures :"+log.failure);
		}
		}catch (IOException e) {
			System.out.println(e.getStackTrace()[0].getLineNumber());
			System.out.println("\nException :"+e);
		}catch (Exception e){  
		System.out.println(e.getStackTrace()[0].getLineNumber());
		System.out.println("\nException :"+e);
		}
	}
 }